package com.hc.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.hc.Operation;
import com.hc.bean.SysRoleBean;
import com.hc.service.SysRoleService;
import com.hc.service.SysRoleServiceImpl;
import com.hc.util.JsonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/sysRoleController")
public class SysRoleController {


    Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    SysRoleService sysRoleService;//  = new SysRoleServiceImpl();

    @GetMapping("/queryData")
    @Operation("查询角色")
    @SaCheckPermission("sys:role:query")
    public JsonResponse queryData(SysRoleBean sysRoleBean){

        //StpUtil.checkPermission("sys:role:query");

       // System.out.println("SysRoleService接口所指向对象的类名为:"+sysRoleService.getClass().getName());
        logger.debug("SysRoleService接口所指向对象的类名为:"+sysRoleService.getClass().getName());
      //  SysRoleService接口所指向对象的类名为:com.hc.service.SysRoleServiceImpl
        //SysRoleService接口所指向对象的类名为:com.hc.service.SysRoleServiceImpl$$SpringCGLIB$$0 //代理类(事务管理)
        JsonResponse jsonResponse = new JsonResponse();

             List<SysRoleBean> sysRoleBeanList = sysRoleService.queryData(sysRoleBean);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");
            jsonResponse.setData(sysRoleBeanList);

        return  jsonResponse;
    }



    @GetMapping(value = "/queryDataByPage" )
    @Operation("分页查询角色")
    @SaCheckPermission("sys:role:query")
    public JsonResponse queryDataByPage(SysRoleBean sysRoleBean,
                                      @RequestParam(required = false,name = "pageNum",defaultValue = "1") int pageNum,
                                       @RequestParam(required = false,name = "pageSize",defaultValue = "5")  int pageSize){
      //  System.out.println("SysRoleService接口所指向对象的类名为:"+sysRoleService.getClass().getName());
        logger.debug("SysRoleService接口所指向对象的类名为:"+sysRoleService.getClass().getName());
       // StpUtil.checkPermission("sys:role:query");
        JsonResponse jsonResponse = new JsonResponse();
        try {
            PageInfo<SysRoleBean> page = sysRoleService.queryDataByPage(sysRoleBean,pageNum,pageSize);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");
            jsonResponse.setData(page);



        }catch (Exception e){
            logger.error("分页查询角色出错了",e);
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());

        }

        return  jsonResponse;
    }



    @PostMapping(value = "/saveRole" )
    @Operation("添加角色")
    @SaCheckPermission("sys:role:insert")
    public JsonResponse saveRole(@RequestBody SysRoleBean sysRoleBean){
       // StpUtil.checkPermission("sys:role:insert");
        JsonResponse jsonResponse = new JsonResponse();
        try {
           sysRoleService.insertData(sysRoleBean);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");



        }catch (Exception e){
          //  e.printStackTrace();
            logger.error("分页查询角色出错了",e);
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());

        }

        return  jsonResponse;
    }


    @PutMapping (value = "/updateRole" )
    @Operation("添加角色")
    @SaCheckPermission("sys:role:update")
    public JsonResponse updateRole(@RequestBody SysRoleBean sysRoleBean){
        //StpUtil.checkPermission("sys:role:update");


        JsonResponse jsonResponse = new JsonResponse();
        try {
            int res = sysRoleService.updateData(sysRoleBean);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");



        }catch (Exception e){
            //  e.printStackTrace();
            logger.error("分页查询角色出错了",e);
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());

        }

        return  jsonResponse;
    }
}
